import {action, computed, observable} from 'mobx';
import {WeaForm, WeaTableNew} from "comsMobx";
import * as API_WFREPORT from '../../apis/efficiencyReport';

const {TableStore} = WeaTableNew;

export class FlowReportDetailStore {

    @observable tableStore = new TableStore();
    @observable loading = false;
    dataKey = "";

    @observable queryParams = {};

    @observable selectTabKey = 1;
    @observable exportExcelUrl = '';

    //是否需要高级搜索
    needAdvacnceSearch = false;
    @observable form = new WeaForm();
    @observable showSearchAd = false;
    @observable condition = [];

    @observable pathProps = {};

    @computed get currentProps() {
        const {currentIndex = 0} = this.pathProps;
        let currentProps = {};
        if (currentIndex > 0) {
            currentProps = this.pathProps[currentIndex];
        }
        return currentProps;
    }
    @observable versionTabs = [];
    @computed get currentPropIndex() {
        const {currentIndex = 0} = this.pathProps;
        return currentIndex;
    }

    constructor() {
        this.updateQueryParams = this.updateQueryParams.bind(this);
        this.doSearch = this.doSearch.bind(this);
        this.changeTab = this.changeTab.bind(this);
        this.exportExcel = this.exportExcel.bind(this);
        this.loadCondition = this.loadCondition.bind(this);
        this.setShowSearchAd = this.setShowSearchAd.bind(this);
        this.setFormFields = this.setFormFields.bind(this);
        this.clearFormFields = this.clearFormFields.bind(this);
        this.clearTableStore = this.clearTableStore.bind(this);
        this.setNeedAdvacnceSearch = this.setNeedAdvacnceSearch.bind(this);
        this.goTnto = this.goTnto.bind(this);
        this.goBack = this.goBack.bind(this);
        this.rewriteTableStore();
    }

    rewriteTableStore = () =>{
        const getDatas = this.tableStore.getDatas.bind(this.tableStore);//重写组件getdatas方法，处理点击列排序、切页时不走回调
        this.tableStore.getDatas = (...arg) => {
            getDatas(arg[0], arg[1], arg[2], arg[3], (...res) => {
                this.showUnoperators(arg[4]);
            }, arg[5]);
        }
    }

    showUnoperators(callback){
        if(callback && typeof(callback) === 'function'){
            callback();//原tablestore的callback
        }
        let newDatas = [];//[{requestid:1,userid:1,unoperators:1},{requestid:2,userid:2,unoperators:2}]
        if(this.tableStore.datas){
            this.tableStore.datas.map(data => {
                newDatas.push({"requestid":data.requestid,"userid":data.userid,"unoperators":data.unoperators});
            })
        }
        API_WFREPORT.getUnoperators({datas:JSON.stringify(newDatas),actiontype:'batch',showAll:'false'}).then(unoperators =>{
            for(var unopt in unoperators){
                jQuery("#"+unopt).html(unoperators[unopt]);
            }
        });
    }

    @action
    doSearch(para = {}) {
        this.loading = true;
        this.tableStore.tableUpdate({loading: true});
        this.updateQueryParams(para);
        let searchCommonParams = {};
        if (this.needAdvacnceSearch) {
            searchCommonParams = this.form.getFormParams();
        }
        API_WFREPORT.getReportDetailList({...this.queryParams, ...searchCommonParams}).then(data => {
            const {versionTabs = [],sessionkey = ''} = data;
            if(versionTabs.length > 0) {
                this.versionTabs = versionTabs;
                this.selectTabKey = versionTabs[0].key;
            }
            this.tableStore.getDatas(sessionkey, para.current || 1);
            this.dataKey = sessionkey;
            this.loading = false;
        });
    }

    changeTab(selectTabKey) {
        this.selectTabKey = selectTabKey;
        if(this.versionTabs.length > 0) {
            this.updateQueryParams({workflowversionid: selectTabKey,isLoadVersionTab:0});
        } else {
            let flowStatus = '';
            if (selectTabKey == 2) flowStatus = 0;
            if (selectTabKey == 3) flowStatus = 1;
            this.updateQueryParams({flowStatus: flowStatus});
        }
    }

    updateQueryParams(para = {}) {
        this.queryParams = {...this.queryParams, ...para};
    }

    clearTableStore() {
        this.tableStore = new TableStore();
        this.rewriteTableStore();
        this.exportExcelUrl = '';
        this.queryParams = {};
        this.pathProps = {};
        this.showSearchAd = false;
        this.versionTabs = [];
        this.selectTabKey  = '1';
    }

    exportExcel(bool = true,filename = '') {
        if (bool) {
            const {api='',module='',operatorid='',keyid='',analyseType=''} = this.queryParams
            // this.exportExcelUrl = '/spa/workflowReport/exportExcel.jsp?tableKey=' + this.dataKey + '&filename=' + filename +'&_t=' + (new Date().getTime());
            this.exportExcelUrl  = encodeURI('/api/report/workflow/exportExcel?tableKey='+this.dataKey+'&filename='+filename+'&_t='+(new Date().getTime())+'&api='+api+'&module='+module+'&operatorid='+operatorid+'&keyid='+keyid+'&analyseType='+analyseType);
            //API_WFREPORT.exportExcel({filename,tableKey:this.dataKey});
        } else {
            this.exportExcelUrl = '';
        }
    }

    loadCondition() {
        this.loading = true;
        API_WFREPORT.getQueryCondition(this.reportParams).then((data) => {
            this.loading = false;
            let condition = data.condition;
            this.condition = condition;
            this.form.initFormFields(condition);
        });
    }

    setNeedAdvacnceSearch(bool) {
        this.needAdvacnceSearch = bool;
    }

    setShowSearchAd(bool) {
        this.showSearchAd = bool;
    }

    setFormFields(value) {
        this.form.updateFields(value, true);    //true代表完全覆盖方式更新条件值
    }

    clearFormFields() {
        this.form.reset(); //清除查询条件值
    }

    //记录参数变化
    goTnto(nextProps) {
        let {currentIndex = 0} = this.pathProps;
        currentIndex = currentIndex + 1;
        let updateProps = {};
        const {modalVisible = false} = nextProps;
        if(modalVisible) {
            updateProps[currentIndex] = nextProps;
            updateProps.currentIndex = currentIndex;
            this.pathProps = {...this.pathProps, ...updateProps}
        } else {
            this.pathProps = {};
        }
    }

    //返回
    goBack() {
        let {currentIndex = 0} = this.pathProps;
        if (currentIndex > 0) {
            delete this.pathProps[currentIndex];
            this.pathProps = {...this.pathProps, 'currentIndex': currentIndex - 1}

            const {dataParams = {},needAdvacnceSearch = false} = this.currentProps;
            this.setNeedAdvacnceSearch(needAdvacnceSearch);
            this.queryParams = {...dataParams, baseSearch: ''};
            this.versionTabs = [];
            this.selectTabKey = '';
            this.doSearch({});
        }
    }
}